package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@RefreshScope
public class BrandController {

    @Autowired
    private BrandService bs;

    //分页+模糊查询
    @PostMapping("brandList")
    public Result brandList(@RequestBody PageParms<BrandVO> parms){
        try{
            PageResult<BrandVO> page = bs.brandList(parms);
            return Result.OK().setData(page);
        }catch (Exception e){
            return Result.FALL();
        }
    }

    //增加
    @PostMapping("addBrand")
    public Result addBrand(@RequestBody BrandVO brandVO){
        try{
            bs.addBrand(brandVO);
            return Result.OK();
        }catch (Exception e){
            return Result.FALL();
        }
    }

    //修改
    @PostMapping("updateBrand")
    public Result updateBrand(@RequestBody BrandVO brandVO){
        try{
            bs.updateBrand(brandVO);
            return Result.OK();
        }catch (Exception e){
            return Result.FALL();
        }
    }

    //删除
    @PostMapping("deleteById")
    public Result deleteById(@RequestParam Long id){
        try{
            bs.deleteById(id);
            return Result.OK();
        }catch (Exception e){
            return Result.FALL();
        }
    }

    //查询一条数据
    @PostMapping("selectById")
    public Result selectById(@RequestParam Long id){
        try{
            BrandVO brandVO = bs.selectById(id);
            return Result.OK().setData(brandVO);
        }catch (Exception e){
            return Result.FALL();
        }
    }

    @PostMapping("selectAll")
    public Result selectAll(){
        List<BrandVO> page = bs.selectAll();
        return Result.OK().setData(page);
    }

}
